//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __SHAREMEM_H__
#define __SHAREMEM_H__

typedef struct _MemoryOwner : public DLinkNode
{
    CProcess * m_pOwner;
    Int32      m_cRef;
} MemoryOwner;

class CSharedMemory : public ISharedMemory, public VDataSource
{
// Interface functions
//
public:
    CARAPI_(PInterface) Probe(
        /* [in] */ REIID riid);

    CARAPI_(UInt32) AddRef(void);

    CARAPI_(UInt32) Release(void);

    CARAPI Aggregate(
        /* [in] */ AggrType aggrType,
        /* [in] */ PInterface pObject);

    CARAPI GetDomain(
        /* [out] */ PInterface *ppObj);

    CARAPI Attach(
        /* [in] */ MemoryProtection protect,
        /* [out] */ Address * pVirtualAddress);

    CARAPI Detach();

// member functions
//
public:
    CSharedMemory();
    virtual ~CSharedMemory();

    CARAPI AllocGlobalAS(Int32 nSize, Boolean bCommit);
    void FreeGlobalAS();

    PPage * GetPage(UInt32 uOffset);
    ECode Flush();

    ECode Detach(CProcess * pCPro, Boolean NeedProcDetach, Boolean Force);

private:
    Int32       m_nSize;
    Address     m_baseAddr;
    Interlocked m_cRef;
    DLinkNode   m_memoryOwnerList;
    KMutex      m_memOwnerLock;
};

inline CSharedMemory::CSharedMemory() :
    m_nSize(0),
    m_baseAddr(NULL)
#ifdef DEBUG_KMUTEX
    ,m_memOwnerLock(__FILE__, __LINE__)
#endif //DEBUG_KMUTEX
{
    m_memoryOwnerList.Initialize();
}

#endif //!(__SHAREMEM_H__)
